<?php

class site_config_model extends CI_Model {

    protected $_instance = array(
        'table' => 'site_config',
        'primaryKey' => 'id',
    );
    protected $_parent = null;
    public $_data = null;

    public function get_config() {
        $config = new stdClass();
        $query = $this->db->get($this->_instance['table']);
        foreach ($query->result() as $row) {
            $field = $row->field;
            $value = $row->value;
            $config->$field = $value;
        }
        $query->free_result();
        return $config;
    }

    public function get_logo_path() {
        $query = $this->db->query('SELECT value FROM ' . $this->_instance['table'] . ' WHERE field LIKE "logo" LIMIT 0, 1');
        $row = $query->row();
        $query->free_result();
        return $row->value;
    }

    public function save_config($data) {
        $data = $this->auto_render($data);
        $this->db->query('DELETE FROM ' . $this->_instance['table']);
        foreach ($data as $field => $value) {
            $this->db->insert($this->_instance['table'], array('field' => $field, 'value' => $value), 'field = "'.$field.'"');
        }
        return $this;
    }

    private function auto_render($data) {
        return $data;
    }

}

?>